﻿#include "MetaCoup.h"

MetaCoup::MetaCoup() : fcsptr(std::make_shared<Focus>()), res(NULL)
{

}

MetaCoup::~MetaCoup()
{
	if (res) {
		free(res);
	}
}

int MetaCoup::preprocess(cv::Mat& mat, Attribute& attr) {
	return Auxpreprocess(mat, attr);
}

int MetaCoup::Auxpreprocess(cv::Mat& mat, Attribute& attr)
{	
	if (0 == fcsptr->preprocess(mat, attr)) {
		switch (attr.type) {
		case Circle:
			return preprocessCircle(mat, attr);
		case Rect:
			return preprocessRect(mat, attr);
		default:
			break;
		}
	}
	return -4;
}

int MetaCoup::findMaxOutline(cv::Mat& mat) {
	if (closed.empty()) {
		return -1; 
	}
	return fcsptr->findMaxOutline(closed);
}

int MetaCoup::findMaxOutline(cv::Mat& mat, Attribute& attr) {
	if (closed.empty()) {
		return -1;
	}
	return fcsptr->findMaxOutline(closed, attr);
}

std::pair<std::vector<cv::Point>, std::vector<cv::Point>> MetaCoup::getTwoRoundOutlines()
{	
	contours_ = fcsptr->getContours();
	std::pair<std::vector<cv::Point>, std::vector<cv::Point>> res_;
	if (contours_.size() > 3) {
		contours_.resize(2);
		res_.first = contours_[0];
		res_.second = contours_[1];
	}
	return res_;
}

int MetaCoup::draw(cv::Mat& mat, Attribute& attr) {
	return fcsptr->draw(mat, attr);	
}

cv::Point2f MetaCoup::getMaxOutlineCenter(Attribute& attr) const {
	return fcsptr->getMaxOutlineCenter(attr);
}

cv::Point2f* MetaCoup::getTwoRoundholeCenter(cv::Mat& mat, Attribute& attr)
{	
	auto round = MetaCoup::getTwoRoundOutlines();
	if (!round.first.empty() && !round.second.empty() && attr.type == Shapetype::Circle) {
		res = (cv::Point2f*)malloc(sizeof(cv::Point2f) * M);
		float radius = 0.0;
		cv::minEnclosingCircle(round.first,
			res[0],
			radius);
		cv::minEnclosingCircle(round.first,
			res[1],
			radius);
	}
	return res;
}

int MetaCoup::preprocessCircle(cv::Mat& mat, Attribute& attr) {
	if (attr.color == Matcolor::ColorNone) {
		return -3;
	}
	
	//...

	return 0;
}

int MetaCoup::preprocessRect(cv::Mat& mat, Attribute& attr) {
	if (attr.color == Matcolor::ColorNone) {
		return -3;
	}
	
	//...

	return 0;
}
